/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bc;

import edu.upn.sigemfi.plt.bc.TarjetaDebitoFacadeLocal;
import edu.upn.sigemfi.plt.be.TarjetaDebito;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author LuMiMan
 */
public class TarjetaDebitoDelegate {

    private TarjetaDebitoFacadeLocal local;

    public TarjetaDebitoDelegate() {
        local = lookupTarjetaDebitoFacade();
    }

    public TarjetaDebito buscar(TarjetaDebito tarjetadebito) throws Exception {
        try {
            return local.find(tarjetadebito.getIdTarjeta());
        } catch (Exception e) {
            local = lookupTarjetaDebitoFacade();
            throw new Exception("Buscar Tarjeta. " + e.getMessage(), e);
        }
    }

    public void crear(TarjetaDebito tarjetadebito) throws Exception {

        try {
            local.create(tarjetadebito);
        } catch (Exception ex) {
            local = lookupTarjetaDebitoFacade();
            throw new Exception("Ingresar Tarjeta... " + ex.getMessage());
        }

    }

    public void eliminar(TarjetaDebito tarjetadebito) throws Exception {
        try {
            local.remove(tarjetadebito);
        } catch (Exception ex) {
            local = lookupTarjetaDebitoFacade();
            throw new Exception("Eliminar Tarjeta... " + ex.getMessage());
        }
    }

    public void editar(TarjetaDebito tarjetadebito) throws Exception {
        try {
            local.edit(tarjetadebito);
        } catch (Exception ex) {
            local = lookupTarjetaDebitoFacade();
            throw new Exception("Editar Tarjeta... " + ex.getMessage());
        }
    }

    public TarjetaDebito buscartarjetaporclave(String clave) throws Exception {
        TarjetaDebito tarjetadebito = null;
        try {
            tarjetadebito = local.find(clave);
        } catch (Exception ex) {
            local = lookupTarjetaDebitoFacade();
            throw new Exception("Buscar tarjeta por clave... " + ex.getMessage());
        }

        return tarjetadebito;
    }

    private TarjetaDebitoFacadeLocal lookupTarjetaDebitoFacade() {
        try {
            Context c = new InitialContext();
            return (TarjetaDebitoFacadeLocal) c.lookup("java:comp/env/TarjetaDebitoFacade");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }
}
